#include <QIcon>
#include <QDebug>
#include <QMessageBox>

#include "manager.h"
#include "ui_manager.h"

Manager::Manager(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Manager),
    settings(new QSettings(QSettings::UserScope, "Climbing wall", "cwall", this)),
    database(QSqlDatabase::addDatabase("QMYSQL", "cwall_db")),
    competition_wizard(new CompetitionPropertesWizard(database, this)),
    cfg_dialog(new ConfigureDialog(settings)),
    memberReg(new FormMemberRegistration(&database, this)),
    memberAndTeam(new FormMemberAndTeam(database, this)),
    groups(new FormGroup(database, this))
{
    ui->setupUi(this);
    setWindowTitle(tr("Climbing wall - competition support"));

    // set menu icons
    // menu manager
    ui->actionManagerConfigure->setIcon(QIcon::fromTheme("configure",QIcon::fromTheme("preferences-system")));
    QIcon *iconDbConnect = new QIcon();
    iconDbConnect->addPixmap(QIcon::fromTheme("network-connect",QIcon::fromTheme("network-wired")).pixmap(22,22), QIcon::Normal, QIcon::On);
    iconDbConnect->addPixmap(QIcon::fromTheme("network-disconnect",QIcon::fromTheme("network-wired-disconnected")).pixmap(22,22), QIcon::Normal, QIcon::Off);
    ui->actionManagerConnect_to_database->setIcon(*iconDbConnect);
    ui->actionManagerExit->setIcon(QIcon::fromTheme("application-exit"));

    // menu competition
    ui->actionCompetitionCreate->setIcon(QIcon::fromTheme("document-new"));
    ui->actionCompetitionOpen->setIcon(QIcon::fromTheme("document-open"));
    ui->actionCompetitionClose->setIcon(QIcon::fromTheme("document-close",QIcon::fromTheme("window-close")));
    ui->actionCompetitionProperties->setIcon(QIcon::fromTheme("document-properties"));
    ui->actionCompetitionMember_registration->setIcon(QIcon::fromTheme("user-group-new"));

    // menu tour

    // menu report
    ui->actionReportStart->setIcon(QIcon::fromTheme("mail-mark-task"));
    ui->actionReportCommit->setIcon(QIcon::fromTheme("mail-mark-notjunk"));
    ui->actionReportReturn_to_edit->setIcon(QIcon::fromTheme("edit-undo"));
    ui->actionReportPrint->setIcon(QIcon::fromTheme("document-print"));
    ui->actionReportView->setIcon(QIcon::fromTheme("document-preview",QIcon::fromTheme("document-print-preview")));

    //menu database
    ui->actionDBaseMembers->setIcon(QIcon::fromTheme("meeting-attending"));
    ui->actionDBaseTeams->setIcon(QIcon::fromTheme("flag-black"));
    ui->actionDBaseGroup->setIcon(QIcon::fromTheme("view-process-users"));
    ui->actionDBaseRange->setIcon(QIcon::fromTheme("view-statistics"));

    //menu window
    QIcon *iconWindowFullScreen = new QIcon();
    iconWindowFullScreen->addPixmap(QIcon::fromTheme("view-fullscreen").pixmap(22,22),QIcon::Normal, QIcon::Off);
    iconWindowFullScreen->addPixmap(QIcon::fromTheme("view-restore").pixmap(22,22),QIcon::Normal, QIcon::On);
    ui->actionWindowFull_screen->setIcon(*iconWindowFullScreen);
    ui->actionWindowClose->setIcon(QIcon::fromTheme("window-close"));
    ui->actionWindowClose_all->setIcon(QIcon::fromTheme("window-close"));
    ui->actionWindowCascade->setIcon(QIcon::fromTheme("window-duplicate"));
    ui->actionWindowTile->setIcon(QIcon::fromTheme("view-form-table"));

    //menu help
    ui->actionAbout_Qt->setIcon(QPixmap("qt-help"));
    ui->actionHelpAbout->setIcon(QIcon::fromTheme("help-about"));
    ui->actionHelpIndex->setIcon(QIcon::fromTheme("help-contents"));
    ui->actionHelpClimbing_competition_rules->setIcon(QPixmap(":/icf"));

    // set connections
    connect(ui->actionManagerConfigure, SIGNAL(triggered()), cfg_dialog, SLOT(exec()));
    connect(ui->actionCompetitionMember_registration, SIGNAL(triggered()), memberReg, SLOT(showMaximized()));

    connect(ui->actionCompetitionCreate, SIGNAL(triggered()), competition_wizard, SLOT(exec()));

    connect(ui->actionDBaseMembers, SIGNAL(triggered()), this, SLOT(memberAndTeamShow()));

    connect(ui->actionDBaseGroup, SIGNAL(triggered()), groups, SLOT(showMaximized()));

    connect(this, SIGNAL(dbOpend(bool)), memberReg, SLOT(setEnabled(bool)));

    QMdiSubWindow *mreg = new QMdiSubWindow(this);
    mreg->setWidget(memberReg);
    ui->mdiArea->addSubWindow(mreg);
    mreg->close();
    mreg->setWindowIcon(ui->actionCompetitionMember_registration->icon());
    QMdiSubWindow *membandteam = new QMdiSubWindow(this);
    membandteam->setWidget(memberAndTeam);
    ui->mdiArea->addSubWindow(membandteam);
    membandteam->close();
    membandteam->setWindowIcon(ui->actionDBaseMembers->icon());
    connect(memberAndTeam, SIGNAL(closed()), membandteam, SLOT(close()));
    QMdiSubWindow *groupsWindow = new QMdiSubWindow(this);
    groupsWindow->setWidget(groups);
    ui->mdiArea->addSubWindow(groupsWindow);
    groupsWindow->close();
    groupsWindow->setWindowIcon(ui->actionDBaseGroup->icon());

    settings->beginGroup("workPlace");
    ui->actionManagerConnect_to_database->setChecked(settings->value("autoconnect", true).toBool());
    on_actionManagerConnect_to_database_triggered(ui->actionManagerConnect_to_database->isChecked());
    settings->endGroup();
}

Manager::~Manager()
{
    delete ui;
}

void Manager::on_actionWindowFull_screen_triggered(bool isFullScreen)
{
    if (isFullScreen) {
        showFullScreen();
    } else {
        showNormal();
    }
}

void Manager::on_actionManagerConnect_to_database_triggered(bool isConnect)
{
    if (isConnect) {
        if (database.isOpen()) {
            database.close();
            database.setHostName("");
            database.setPassword("");
            database.setUserName("");
            database.setDatabaseName("");
        }
        QString host;
        QString base;
        QString user;
        QString passwd;
        cfg_dialog->mainConnection(host, base, user, passwd);

        database.setHostName(host);
        database.setDatabaseName(base);
        database.setUserName(user);
        database.setPassword(passwd);
        if (database.isValid()) database.open();
        if (!database.isOpen()) {
            QMessageBox::critical(this, tr("Database error"), tr("Error open database") +
                                  "mysql://" + user + "@" + host + "/" + base,
                                  QMessageBox::Close, QMessageBox::Close);
            isConnect = false;
            ui->actionManagerConnect_to_database->setChecked(false);
        }
    } else {
        if (database.isOpen()) database.close();
        /// @todo set test for data changed and recommend user update data
    }
    ui->menuCompetition->setEnabled(isConnect);
    ui->menuDatabase->setEnabled(isConnect);
    ui->menuReport->setEnabled(isConnect);
    ui->menuTour->setEnabled(isConnect);
    ui->toolBarCompetition->setEnabled(isConnect);
    ui->toolBarDatabase->setEnabled(isConnect);
    ui->toolBarReport->setEnabled(isConnect);
    ui->toolBarTour->setEnabled(isConnect);
    settings->beginGroup("workPlace");
    settings->setValue("autoconnect", isConnect);
    settings->endGroup();
    emit dbOpend(isConnect);
}

void Manager::on_actionReportPrint_triggered()
{

}
void Manager::memberAndTeamShow()
{
    if (!memberAndTeam->isVisible()) {
        memberAndTeam->MemberUpdate();
        memberAndTeam->TeamUpdate();
    }
    memberAndTeam->showMaximized();
}

void Manager::on_actionAbout_Qt_triggered()
{
    QApplication::aboutQt();
}
